from D5Core.D5Lights import PointLight
from D5Core.D5Models.Details.D5ModelBase import D5Model
from D5Core.D5Common import *
from D5Core.Details.D5Api import D5Api, APIRequestType
import json
from typing import Dict

class ModelFactory:
    """模型工厂基类，负责创建特定类型的模型"""
    
    def create(self, dict_data: Dict) -> D5Model:
        """创建模型实例"""
        pass

class DefaultModelFactory(ModelFactory):
    """默认模型工厂，创建D5Model实例"""
    
    def create(self, dict_data: Dict) -> D5Model:
        return D5Model(dict_data)

class PointLightFactory(ModelFactory):
    """光源模型工厂，创建D5Light实例"""
    
    def create(self, dict_data: Dict) -> D5Model:
        return PointLight(dict_data)


class D5ModelCreator:
    """模型创建器，使用工厂模式创建不同类型的模型"""
    
    # 模型工厂注册表
    _factories: Dict[D5ApiActorType, ModelFactory] = {}
    
    @classmethod
    def register_factory(cls, object_type: D5ApiActorType, factory: ModelFactory) -> None:
        """注册特定类型的模型工厂"""
        cls._factories[object_type] = factory
    
    @classmethod
    def get_factory(cls, object_type: D5ApiActorType) -> ModelFactory:
        """获取特定类型的模型工厂，如果未找到则返回默认工厂"""
        return cls._factories.get(object_type, DefaultModelFactory())
    
    @classmethod
    def create_model(cls, dict_data: Dict) -> D5Model:
        """创建模型实例"""
        unique_actor_id = dict_data.get("id", "")
        object_type = D5ApiActorType.from_str(dict_data.get("object_type", D5ApiActorType.NoType.to_str()))
        
        
        model_data = D5Api().get_model_data(unique_actor_id)
        dict_data.update(model_data)
        object_type = D5ApiActorType.from_str(dict_data.get("object_type", D5ApiActorType.NoType.to_str()))
        
        # 使用对应的工厂创建模型
        factory = cls.get_factory(object_type)
        model = factory.create(dict_data)
        return model

# 注册默认的模型工厂
D5ModelCreator.register_factory(D5ApiActorType.PointLight, PointLightFactory())
